package org.example.demopaper.controller;

import org.example.demopaper.entity.Paper;
import org.example.demopaper.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/paper")
@CrossOrigin(origins = "http://localhost:63342/")
public class PaperController {
    @Autowired
    private PaperService paperService;
    // 查询数据库里的论文数据列表
    @GetMapping("/paperList")
    public List<Paper> getPaperList() {
        return paperService.getAllPapers();
    }
    // 根据论文的id来查询paper
    @GetMapping("/getPaperById")
    public Paper getPaperById(@RequestParam("id") int id) {
        return paperService.getPaperById(id);
    }
    // 删除paper
    @DeleteMapping("/delete/{id}")
    public String deletePaper(@PathVariable("id") int id) {
        if (paperService.deletePaper(id) == 1) {
            return "ok";
        } else {
            return "error";
        }
    }
    // 添加paper
    @PostMapping("/add")
    public String addPaper(@RequestBody Paper paper) {
        if (paperService.insertPaper(paper) == 1) {
            return "ok";
        } else {
            return "error";
        }
    }
    // 编辑paper
    @PutMapping("/edit")
    public String editPaper(@RequestBody Paper paper) {
        if (paperService.editPaper(paper) == 1) {
            return "ok";
        } else {
            return "error";
        }
    }
}